package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.Bukkit;

import java.lang.reflect.Field;

public interface CanSetUp extends ConfigurationSectionCanLoad {
    @Override
    default void load(ConfigurationSectionSetDefGet set){
        for (Field declaredField : this.getClass().getDeclaredFields()) {
            CanSetUpLang canSetUpLang =  declaredField.getAnnotation(CanSetUpLang.class);
            if (canSetUpLang !=null){
                lang(declaredField,set);
                continue;
            }
            CanSetUpSetUp canSetUpSetUp =  declaredField.getAnnotation(CanSetUpSetUp.class);
            if (canSetUpSetUp !=null){
                setUp(declaredField,set);
                continue;
            }
        }
    }

    default void setUp(Field declaredField, ConfigurationSectionSetDefGet set){
        declaredField.setAccessible(true);
        Class type = declaredField.getType();
        if (type.equals(String.class)){
            try {
                declaredField.set(this,set.getStringSetDef("setUp."+declaredField.getName(), (String) declaredField.get(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else if (type.equals(int.class)){
            try {
                declaredField.setInt(this,set.getIntSetDef("setUp."+declaredField.getName(), declaredField.getInt(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else if (type.equals(long.class)){
            try {
                declaredField.setLong(this,set.getLongSetDef("setUp."+declaredField.getName(), declaredField.getLong(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else if (type.equals(double.class)){
            try {
                declaredField.setDouble(this,set.getDoubleSetDef("setUp."+declaredField.getName(), declaredField.getDouble(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else if (type.equals(boolean.class)){
            try {
                declaredField.setBoolean(this,set.getBooleanSetDef("setUp."+declaredField.getName(), declaredField.getBoolean(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else{
            Bukkit.getLogger().warning("NeedSet不支持"+type);
        }
    }


    default void lang(Field declaredField,ConfigurationSectionSetDefGet set){
        declaredField.setAccessible(true);
        Class type = declaredField.getType();
        if (type.equals(String.class)){
            try {
                declaredField.set(this,set.getStringSetDef("lang."+declaredField.getName(), (String) declaredField.get(this)));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }else{
            Bukkit.getLogger().warning("NeedSet不支持"+type);
        }
    }
}
